#!/usr/bin/env python
#coding: utf-8
############################
__author__ = "kauu (kauu@yahoo.cn)"
__version__ = "1.0"
__copyright__ = "Copyright (c) 2008 "
__license__ = "NO"

from crawldb import DB_crawl
import md5,time
from util import *
from datetime import datetime
import pg
import logging as log

class Car:
      def __init__(self):		
		self.carid=0
		self.type='' 		#type
		self.pattern='' 	#款式
		self.disposition='' 	#配置
		self.factory=0 		#国产/进口
		self.color = '' 
		self.price =0 
		self.manue =0  		#手动，自动，手自动
		self.distance = '' 	#行使路程
		self.seats =0
		self.owner=''
		self.owner_link=''
		self.ownership = ''  	#可否过户
		self.phone =''		#电话
		self.mobile  = ''	#手机
		self.time =''		 #使用年限
		self.publish='' 	 #发布时间
		self.location=''
		self.introduction=''	   
		self.link=''	   
      def init(self):
		self.__init_()

      def __str__(self):
		self.toString()

      def toString(self):
		print	'owner:',self.owner,'\n'\
			'owner_link:',self.owner_link,'\n'\
			'link:',self.link,'\n'\
			'publish:',self.publish,'\n'\
			'type:',self.type,'\n'\
			'location:',self.location,'\n'		       \
			'phone:',self.phone,'\n' \
			'mobile:',self.mobile, '\n' \
			'introduction:',self.introduction,'\n'									
			
      def serialize(self):
        """
        @author: 
        @param self:
        @type self:
        @return:
        @rtype:
        """
        return None

      def save(self,db = None):
		if db == None:
			db = DB_crawl()	
			not_close = 1
		else:
			not_close =0
		now = str(time.time())
		uuid = md5.new(self.link).hexdigest()
		owner_id = md5.new(self.link+self.owner_link).hexdigest()
		publish = self.publish
		city_id = md5.new(self.location).hexdigest()
		record = {'uid':uuid,\
			'car_id':self.carid,\
			'pattern':g2u(self.pattern),\
			'disposition':g2u(self.disposition), \
			'factory':g2u(self.factory), \
			'color':g2u(self.color), \
			'price':g2u(self.price) , \
			'manue':g2u(self.manue), \
			'distance':g2u(self.distance), \
			'seats': g2u(self.seats), \
			'time': g2u(self.time), \
			'owner_id':owner_id,\
			'owner':g2u(self.owner),\
			'owner_link':self.owner_link,\
			'phone':self.phone,\
			'mobile':self.mobile,\
			'publish':datetime.strptime(self.publish,"%Y-%m-%d %H:%M:%S").now(),\
			'type':g2u(self.type),\
			'city':g2u(self.location),\
			'city_id':0,\
			'introduction':g2u(self.introduction),\
			'link':self.link}	
		try:
			#print record
			db.insert('cars_cars',record)
		except pg.ProgrammingError,string:
			print string
		else:
			log.info('add a record ok ',uuid)
		if not_close == 1:
			db.close()	



